package com.example.bookmanage.controller;

import com.example.bookmanage.pojo.User;
import com.example.bookmanage.service.UserService;
import com.example.bookmanage.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class LoginController {
    private UserService service;
    @Autowired
    public void setService(UserService service) {
        this.service = service;
    }

    /**
     *
     * @param account 前端传递来的账号
     * @param password 密码
     * @return 后端向前段返回一个提示信息 理论上应该返回的是一个封装着json格式的对象
     */
    @RequestMapping("login")
    public JsonResult<Void> login(String account, String password, HttpSession session){
        JsonResult<Void>result=new JsonResult<>();
        User loginUser = service.login(account,password);
        if (loginUser!=null){
            result.setCode(200);
            result.setMsg("登录成功");
            session.setAttribute("user",loginUser);
        }else{
            //将对象封装在session中共所有的类可以进行公用
            result.setMsg("登录失败");
            result.setCode(100);
        }
        return  result;
    }
}
